import axios from 'axios'
import {ElMessage} from 'element-plus'
import router from '../../router'

function postRequest(url, body) {
    return new Promise((resolve, reject) => {
        axios.post(url, body,
            {
                timeout: 2000,
                headers: {
                    "groupId": localStorage.getItem("groupId"),
                    "userId": localStorage.getItem("userId")
                }
            })
            .then((response) => {
                const data = response.data;
                if (data.code === 200) {
                    // ElMessage({
                    //     showClose: true,
                    //     message: data.msg,
                    //     type: "success"
                    // })
                    resolve(data.data)
                } else {
                    ElMessage({
                        showClose: true,
                        message: data.msg,
                        type: "error"
                    })
                }
            })
            .catch((err) => {
                if (err.response.status === 999) {
                    if (err.response.data === "请先登录") {
                        router.push("/login")
                    }
                    ElMessage({
                        showClose: true,
                        message: err.response.data,
                        type: "error"
                    })
                } else {
                    ElMessage({
                        showClose: true,
                        message: "系统未知错误，请联系管理员",
                        type: "error"
                    })
                }
            })
    })
}

export default postRequest